package cn.daniellee.plugin.ib.storage;

import cn.daniellee.plugin.ib.InfiniteBlock;
import cn.daniellee.plugin.ib.model.PlayerData;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.Set;

public class YamlStorage extends Storage {

	private File buildingFile = new File(InfiniteBlock.getInstance().getDataFolder(), "building.yml");

	private FileConfiguration buildingData = new YamlConfiguration();

	@Override
	public boolean initialize() {
		try {
			if (!buildingFile.exists()) buildingFile.createNewFile();
			buildingData.load(buildingFile);
		} catch (Exception e) {
			e.printStackTrace();
			InfiniteBlock.getInstance().getLogger().info(" ");
			InfiniteBlock.getInstance().getLogger().info("&5[InfiniteBlock]An error occurred in building file load.".replace("&", "§"));
			InfiniteBlock.getInstance().getLogger().info(" ");
			return false;
		}
		refreshCache();
		return true;
	}

	@Override
	public void refreshCache() {
		allPlayerData.clear();
		Set<String> ids = buildingData.getKeys(false);
		for (String id : ids) {
			PlayerData playerData = new PlayerData();
			playerData.setId(id);
			playerData.setName(buildingData.getString(id + ".name", "&e&lUnnamed"));
			playerData.setPlayer(buildingData.getString(id + ".player", ""));
			playerData.setServer(buildingData.getString(id + ".server", ""));
			playerData.setLocation(buildingData.getString(id + ".location", "world,0,0,0,0,0"));
			playerData.setCreateDate(buildingData.getLong(id + ".createDate", System.currentTimeMillis()));
			playerData.setReviewed(buildingData.getBoolean(id + ".reviewed", false));
			playerData.setLikes(buildingData.getInt(id + ".likes", 0));
			playerData.setLiker(buildingData.getString(id + ".liker"));
			playerData.setIconMaterial(buildingData.getString(id + ".iconMaterial"));
			playerData.setIconDurability(buildingData.getInt(id + ".iconDurability", 0));
			playerData.setIntroduction(buildingData.getString(id + ".introduction"));
			allPlayerData.add(playerData);
		}
	}

	@Override
	public void addBuilding(PlayerData playerData) {
		String id = getNextId();
		// 写配置
		buildingData.set(id + ".name", playerData.getName());
		buildingData.set(id + ".player", playerData.getPlayer());
		buildingData.set(id + ".server", playerData.getServer());
		buildingData.set(id + ".location", playerData.getLocation());
		buildingData.set(id + ".createDate", playerData.getCreateDate());
		saveBuildingData();
		allPlayerData.add(playerData);
	}

	@Override
	public void updateBuilding(String id, String column, String value) {
		PlayerData playerData = getBuildingById(id);
		if (playerData == null) return;
		switch (column) {
			case "name":
				playerData.setName(value);
				buildingData.set(id + ".name", value);
				break;
			case "server":
				playerData.setServer(value);
				buildingData.set(id + ".server", value);
				break;
			case "location":
				playerData.setLocation(value);
				buildingData.set(id + ".location", value);
				break;
			case "reviewed":
				boolean reviewed = Boolean.parseBoolean(value);
				playerData.setReviewed(reviewed);
				buildingData.set(id + ".reviewed", reviewed);
				break;
			case "likes":
				int likes = Integer.parseInt(value);
				playerData.setLikes(likes);
				buildingData.set(id + ".likes", likes);
				break;
			case "liker":
				playerData.setLiker(value);
				buildingData.set(id + ".liker", value);
				break;
			case "icon_material":
				playerData.setIconMaterial(value);
				buildingData.set(id + ".iconMaterial", value);
				break;
			case "icon_durability":
				int iconDurability = Integer.parseInt(value);
				playerData.setIconDurability(iconDurability);
				buildingData.set(id + ".iconDurability", iconDurability);
				break;
			case "introduction":
				playerData.setIntroduction(value);
				buildingData.set(id + ".introduction", value);
				break;
			default:
				return;
		}
		saveBuildingData();
	}

	@Override
	public void deleteBuilding(String id) {
		PlayerData playerData = getBuildingById(id);
		if (playerData == null) return;
		allPlayerData.remove(playerData);
		buildingData.set(id, null);
		saveBuildingData();
	}

	@Override
	public String getNextId() {
		String nextId;
		if (allPlayerData.size() > 0) {
			nextId = Integer.toString(Integer.parseInt(allPlayerData.get(allPlayerData.size() - 1).getId()) + 1);
		} else nextId = "1";
		return nextId;
	}

	private void saveBuildingData() {
		try {
			buildingData.save(buildingFile);
		} catch (IOException e) {
			InfiniteBlock.getInstance().getLogger().info(" ");
			InfiniteBlock.getInstance().getLogger().info("[InfiniteBlock]An error occurred in player file save.".replace("&", "§"));
			InfiniteBlock.getInstance().getLogger().info(" ");
			e.printStackTrace();
		}
	}

}
